<template>
  <div class="user">
    <div class="header">
      <div class="form">
        <el-form>
          <el-form-item>
            <el-input v-model="keywords" placeholder="请输入用户id或昵称" clearable>
              <el-button slot="append" icon="el-icon-search" @click="getUserByKeyWords"/>
            </el-input>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <div class="main table">
      <el-table :data="list" border fit>
        <el-table-column label="编号" align="center">
          <template slot-scope="scope">
            {{ scope.row.id }}
          </template>
        </el-table-column>

        <el-table-column label="昵称"  align="center">
          <template slot-scope="scope">
            {{ scope.row.nickName }}
          </template>
        </el-table-column>

        <!-- <el-table-column label="姓名"  align="center">
          <template slot-scope="scope">
            {{ scope.row.realName }}
          </template>
        </el-table-column> -->

        <el-table-column label="性别"  align="center">
          <template slot-scope="scope">
            {{ scope.row.gender===1?'男': '女' }}
          </template>
        </el-table-column>

        <el-table-column label="状态"  align="center">
          <template slot-scope="scope">
            {{ scope.row.status===1? '正常':'禁用' }}
          </template>
        </el-table-column>

        <el-table-column label="创建时间"  align="center">
          <template slot-scope="scope">
            {{ scope.row.created_at }}
          </template>
        </el-table-column>

        <el-table-column label="更新时间"  align="center">
          <template slot-scope="scope">
            {{ scope.row.updated_at }}
          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" min-width="200">
          <template slot-scope="scope">
            <el-button type="primary" @click="toUserDetail(scope.row.id)">详情</el-button>
            <el-button type="warning" v-if="scope.row.status===1" @click="opration(scope.row)">禁用</el-button>
            <el-button type="success" v-if="scope.row.status===0" @click="opration(scope.row)">启用</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
    :total="total"
    :page.sync="listQuery.page"
    :limit.sync="listQuery.limit"
    @pagination="getList" />
  </div>
</template>
<script>
import userApi from '@/api/user'
import Pagination from '@/components/Pagination'
export default {
  components: {
    Pagination
  },
  data() {
    return {
      list: [],
      keywords: '',
      total: 0,
      listQuery: {
        page: 1,
        limit: 10
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取全部用户
    async getList(e) {
      const query = {}
      if(e == undefined) {
        query.page = this.listQuery.page - 1,
        query.limit = this.listQuery.limit
      }
      else {
        query.page = e.page - 1,
        query.limit = e.limit
      }
      const result = await userApi.getCommonUsers(query)
      this.list = result.data.rows
      this.total = result.data.count
    },
    // 根据id修改用户状态
    async opration(row) {
      const data = {
        status: 1
      }
      if(row.status===0) {
        await userApi.updateStatus(row.id, data)
        this.$message.success('启用成功')
      }else {
        data.status = 0
        await userApi.updateStatus(row.id, data)
        this.$message.success('禁用成功')
      }
      this.reload()
    },
    // 根据搜索关键字获取用户
    async getUserByKeyWords() {
      if(this.keywords) {              //搜索关键字不为空
        const query = {}
        if(Number(this.keywords)) {
          query.id = this.keywords
        }else {
          query.nickName = this.keywords
        }
        const result = await userApi.getUserByKeyWords(query)
        if(result.code == 0) {
          this.list = []
          this.list = result.data
        }
      }else {                        // 搜索关键字为空
        this.getList()
      }
    },
    // 跳转详情
    toUserDetail(id) {
      this.$router.push({
        name: 'UserDetail',
        params: {id: id}
      })
    }
  },
  inject: ['reload']
}
</script>
<style lang="less" scoped>
  .user {
    margin: 30px;
    position: relative;
    .header {
      display: flex;
      flex-direction: row;
      justify-content: flex-start;
    }
  }
</style>
